package cn.com.dycc.party_work.education_result_dept.service;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.attach.dao.AttachDao;
import cn.com.dycc.common.attach.entity.Attach;
import cn.com.dycc.party_org.organize.dao.OrganizeDao;
import cn.com.dycc.party_org.organize.entity.OrganizeEntity;
import cn.com.dycc.party_work.education_result_dept.dao.EducationResultDeptDao;
import cn.com.dycc.party_work.education_result_dept.entity.EducationResultDept;
import cn.com.dycc.party_work.education_result_dept.entity.EducationResultDeptInfo;
import cn.com.dycc.party_work.education_result_dept.param.EducationResultDeptParam;
import cn.com.dycc.study_manage.integral.dao.Pb_Integral_Dept_LogDao;
import cn.com.dycc.study_manage.integral.dao.Pb_Integral_RuleDao;
import cn.com.dycc.study_manage.integral.entity.Pb_Integral_Dept_Log;
import cn.com.dycc.study_manage.integral.entity.Pb_Integral_Rule;
import cn.com.dycc.study_manage.integral.param.Pb_Integral_RuleParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.FileUtil;
import cn.com.dycc.util.Paging;
import cn.com.dycc.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * Description: 党组织教育学习成果
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年7月4日 上午9:14:14.
 * Update Date Time: 
 * @see
 */
@Service("educationResultDeptService")
public class EducationResultDeptServiceImpl implements EducationResultDeptService{
	@Resource
	private EducationResultDeptDao educationResultDeptDao;
	
	@Resource
	private AttachDao attachDao;
	
	@Resource
	private Pb_Integral_RuleDao pb_Integral_RuleDao;
	
	@Resource
	private Pb_Integral_Dept_LogDao pb_Integral_Dept_LogDao;
	
	@Resource
	private OrganizeDao organizeDao;
	
	@Resource(name="paging")
	private Paging paging;

	public int addEduResultDept(EducationResultDept eduResult, HttpServletRequest req) throws EduResultDeptNotFoundException, UnsupportedEncodingException {
		HttpSession session = req.getSession();
		String user_id = (String) session.getAttribute("id");
		String dept_id = (String) session.getAttribute("dept_id");
		String org_id = (String) session.getAttribute("org_id");
		if(user_id==null){
			throw new EduResultDeptNotFoundException("请重新登录");
		}
		if(eduResult==null){
			throw new EduResultDeptNotFoundException("请填写汇报");
		}
		if(eduResult.getResult_title()==null||eduResult.getResult_title().trim().isEmpty()){
			throw new EduResultDeptNotFoundException("填写标题");
		}
		if(eduResult.getEducation_id()==null||eduResult.getEducation_id().trim().isEmpty()){
			throw new EduResultDeptNotFoundException("填选择教育学习");
		}
		/*if(eduResult.getDept_id()==null||eduResult.getDept_id().trim().isEmpty()){
			throw new EduResultDeptNotFoundException("请填写党组织");
		}*/
		if(eduResult.getPersontime_sum()==null||eduResult.getPersontime_sum().trim().isEmpty()){
			throw new EduResultDeptNotFoundException("请填写学习人次数");
		}
		if(eduResult.getTime_len_sum()==null||eduResult.getTime_len_sum().trim().isEmpty()){
			throw new EduResultDeptNotFoundException("请填写学习时长数");
		}
		if(eduResult.getLearn_sum()==null||eduResult.getLearn_sum().trim().isEmpty()){
			throw new EduResultDeptNotFoundException("请填写组织学习总次数");
		}
		if(eduResult.getReport_des()==null||eduResult.getReport_des().trim().isEmpty()){
			throw new EduResultDeptNotFoundException("请填写汇报成果");

		}
		String des = StringUtil.decode(eduResult.getReport_des());
		
		eduResult.setReport_des(des);
		eduResult.setDept_id(dept_id);
		eduResult.setReport_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		eduResult.setOrg_id((String)session.getAttribute("org_id"));
		eduResult.setOpt_user_id(user_id);
		int n = educationResultDeptDao.addEduResultDept(eduResult);
		if(n!=1){
			throw new EduResultDeptNotFoundException("添加失败");
		}
		
		//积分
		//添加党支部积分
		Pb_Integral_RuleParam param = new Pb_Integral_RuleParam();
		param.setStart(0);
		param.setEnd(1);
		List<Pb_Integral_Rule> listRule = pb_Integral_RuleDao.findPb_Integral_RuleByParam(param);
		Integer integral_val=0;		
		Pb_Integral_Rule rule = listRule.get(0)==null?null:listRule.get(0);
		if(rule!=null){
			integral_val = rule.getLearn_reply_val();
		}
		OrganizeEntity organize = organizeDao.findOrganizeByDeptId(dept_id,org_id);
		
		String log_des = organize==null?"":organize.getDept_name();
		log_des = log_des+"于"+new Timestamp(new Date().getTime())+"反馈教育学习，获取积分"+integral_val+"分";
		
		Pb_Integral_Dept_Log entity = new Pb_Integral_Dept_Log();
		entity.setIntegral_val(integral_val);
		entity.setLog_flag("2");
		entity.setLog_des(log_des);
		entity.setDept_id(dept_id);
		entity.setState("Y");
		entity.setOrg_id((String)session.getAttribute("org_id"));
		entity.setCreate_date(new Timestamp(new Date().getTime()));
		entity.setOpt_user_id(user_id);
		entity.setInfo_id(eduResult.getEducation_id());			
		pb_Integral_Dept_LogDao.savePb_Integral_Dept_Log(entity);
		
		//附件
		List<Attach> listAttach = new ArrayList<Attach>();
		
		if(eduResult.getListAttach()!=null&&!eduResult.getListAttach().trim().isEmpty()){
			//System.out.println("e2="+edu);
			String str=eduResult.getListAttach().trim();
			JSONArray json=JSONArray.fromObject(str);
        
			for(int i=0;i<json.size();i++){

				Object check=json.get(i);
				if("null".equals(check.toString())){
					continue;
				}
				JSONObject job = json.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
				
	
				String attach_ext=(String)job.get("attach_ext");
				String attach_path=(String)job.get("attach_path");
				String attach_size=(String)job.get("attach_size");
				String title=(String)job.get("title");
				
				Attach attach=new Attach();
				attach.setMessage_no(eduResult.getResult_id());
				attach.setAttach_type(8);
				attach.setTitle(title);
				attach.setAttach_ext(attach_ext);
				attach.setAttach_path(attach_path);
				attach.setAttach_size(attach_size);				
				attach.setOrg_id((String)session.getAttribute("org_id"));
				attach.setOpt_user_id(user_id);
				listAttach.add(attach);
								
			}        
        }
		
		List<Attach> newListAttach=FileUtil.moveFile(listAttach,"party_work");
		
		if(newListAttach!=null){
		for(Attach attach:newListAttach){
			attachDao.insertAttach(attach);
				
			}
		}
		return n;
	}

	public EducationResultDept findEduResultDept(String result_id, HttpServletRequest req)
			throws EduResultDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduResultDeptNotFoundException("请重新登录");
		}		
		if(result_id==null||result_id.trim().isEmpty()){
			throw new EduResultDeptNotFoundException("请选择反馈结果");
		}
		
		EducationResultDept edu = educationResultDeptDao.findEduResultDept(result_id,org_id);
		if(edu==null){
			throw new EduResultDeptNotFoundException("查询失败");
		}
		return edu;
	}

	public List<EducationResultDeptInfo> findEduResultDeptByParam(EducationResultDeptParam param,
			HttpServletRequest req) throws EduResultDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduResultDeptNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);
		param.setOrg_id(org_id);
		List<EducationResultDeptInfo> list = educationResultDeptDao.findEduResultDeptByParam(param);
		return list;		
	}

	public String findEduResultDeptCount(EducationResultDeptParam param, HttpServletRequest req)
			throws EduResultDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String) session.getAttribute("org_id");
		if(org_id==null){
			throw new EduResultDeptNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = educationResultDeptDao.findEduResultDeptCount(param);
		return count;
	}

	@Transactional
	public int deleteEduResultDept(String[] result_ids, HttpServletRequest req) throws EduResultDeptNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new EduResultDeptNotFoundException("请重新登录");
		}
		if(result_ids==null){
			throw new EduResultDeptNotFoundException("请选择删除的结果");
		}
		if(result_ids.length<1){
			throw new EduResultDeptNotFoundException("选择删除的结果");
		}
		List<String> list = Arrays.asList(result_ids);
		int n = educationResultDeptDao.deleteEduResultDept(org_id,list);
		if(n!=result_ids.length){
			throw new EduResultDeptNotFoundException("删除失败");
		}
		return n;
	}

	
}
